package org.sharding.config;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.math.BigInteger;
import java.util.Collection;

public class SimplePreciseDBShardingAlgorithm implements PreciseShardingAlgorithm<Long> {

    /**
     * @param collection           表示可选的数据源值 - 这里对应的数据库别名集合
     * @param preciseShardingValue 分片键对应的值，逻辑表名、切片字段、切片字段值
     *
     * @return 需要查询的数据库、表名称集合
     */
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
        // 切片字段
        String columnName = preciseShardingValue.getColumnName();
        // 逻辑表名
        String logicTableName = preciseShardingValue.getLogicTableName();

        if ("course".equals(logicTableName) && "cid".equals(columnName)) {
            Long value = preciseShardingValue.getValue();

            return logicTableName + "_" + (value % 2 + 1);
        }
        throw new RuntimeException("不存在的数据包");
    }
}
